<script setup>
import { message } from 'ant-design-vue'
import dataSourceApi from '@/api/datasource/dataSourceApi'


const props = defineProps(["sourceDsId", "targetDsId", "databaseConfig"])

const tables = ref([]);

const targetKeys = ref([])
const selectedKeys = ref([])

onMounted(async () => {
	tableListToTargetKeys();
	tables.value = await queryTableList();

})


const tableListToTargetKeys = () => {

	if (!props.databaseConfig.tableList || props.databaseConfig.tableList.length <= 0) {
		return;
	}
	for (const c of props.databaseConfig.tableList) {
		targetKeys.value.push(c.sourceTableName)
	}
}


const queryTableList = async () => {
	//查询表
	var s = await dataSourceApi.queryTableList({
		id: props.sourceDsId,
		catalogName: props.databaseConfig?.sourceDatabaseName
	});
	if (s) {
		var d = [];
		s.forEach(v => {
			d.push({
				key: v,
				title: v,
				description: v
			})

		})
		return d;
	}
	return [];

}

const handleChange = () => {
	props.databaseConfig.tableList = [];

	for (let c in targetKeys.value) {
		props.databaseConfig.tableList.push({
			sourceTableName: targetKeys.value[c],
			targetTableName: targetKeys.value[c]
		})
	}
	// console.log(props.databaseConfig.tableList)
}
</script>

<template>


	<a-row :gutter="10" class="mb-2">
		<a-col :span="24">
			<a-card>
				<div style="margin-left: 25%;">
					<a-transfer :showSearch="true" v-model:target-keys="targetKeys" v-model:selected-keys="selectedKeys"
						:data-source="tables" :titles="['来源', '目标']" :render="item => item.title"
						@change="handleChange" />
				</div>
			</a-card>
		</a-col>

	</a-row>



</template>

<style scoped lang="less"></style>
